#region File Description
//-----------------------------------------------------------------------------
// OptionsMenuScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;
#endregion

namespace IcyTowerMobile
{
    /// <summary>
    /// The options screen is brought up over the top of the main menu
    /// screen, and gives the user a chance to configure the game
    /// in various hopefully useful ways.
    /// </summary>
    class OptionsMenuScreen : MenuScreen
    {
        static bool sounds = true;

        private MenuEntry soundsMenuEntry;

        /// <summary>
        /// Constructor.
        /// </summary>
        public OptionsMenuScreen()
            : base("")
        {
            // Create our menu entries.
            soundsMenuEntry = new MenuEntry(string.Empty);

            sounds = IsolatedStorageManager.GetInstance().IsSoundEnabled();

            SetMenuEntryText();

            // Hook up menu event handlers.
            soundsMenuEntry.Selected += SoundsMenuEntrySelected;

            // Add entries to the menu.
            MenuEntries.Add(soundsMenuEntry);
        }


        /// <summary>
        /// Fills in the latest values for the options screen menu text.
        /// </summary>
        void SetMenuEntryText()
        {
            soundsMenuEntry.Text = "Sounds: " + (sounds ? "on" : "off");
        }

        /// <summary>
        /// Event handler for when the Frobnicate menu entry is selected.
        /// </summary>
        void SoundsMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            sounds = !sounds;

            // persist sound setting to isolated storage
            IsolatedStorageManager.GetInstance().SetSoundEnabled(sounds);

            // sounds setting has been toggled, also toggle sound
            FrameworkDispatcher.Update();
            if (MediaPlayer.GameHasControl)
            {
                if (sounds)
                {
                    playSound();
                }
                else
                {
                    MediaPlayer.Stop();
                }
            }
            else
            {
                if (sounds)
                {
                    MessageBoxScreen messageBox = new MessageBoxScreen("Is it ok to stop\ncurrently playing music\nand play our game music?", "Yes", "No");
                    messageBox.Accepted += MusicMessageBoxAccepted;
                    messageBox.Cancelled += MusicMessageBoxCancelled;
                    ScreenManager.AddScreen(messageBox, null);
                }
            }

            // show on UI
            SetMenuEntryText();
        }


        private void playSound()
        {
            MediaPlayer.Stop();
            MediaPlayer.IsRepeating = true;
            MediaPlayer.Volume = 0.3f;
            MediaPlayer.Play(ScreenManager.MenuSong);
        }

        void MusicMessageBoxAccepted(object sender, PlayerIndexEventArgs e)
        {
            playSound();
        }

        void MusicMessageBoxCancelled(object sender, PlayerIndexEventArgs e)
        {
            // do nothing here
        }

    }
}
